<?php

/**
 * p1pronos actions.
 *
 * @package    pronos
 * @subpackage p1pronos
 * @author     sthiebaut
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class p1pronosActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
	$user = $this->getUser();
	$this->forward404Unless($user->isAuthenticated());
	
	$day = idate("d");
	$month = idate("m");
	$hr = 1 + idate("H");
	$min = idate("i");
	$today = date('Y-m-d H:i', mktime($hr,$min,0,$month,$day, 2010));
			
	$this->matchs_date = Doctrine::getTable('PronosP1Match')
	  ->createQuery('a')
	 // ->where("a.date > '".$today."'")
	  ->limit(15)
	  ->orderby('date')
	  ->execute();
	
	$this->form = new EditPronosForm();
		if($user->hasCredential('admin') ) {
		$this->pronos_p1_pronoss = Doctrine::getTable('PronosP1Pronos')
		  ->createQuery('a')
		  ->orderBy('p1match_id')
		  ->execute();	
	} else {
		$this->pronos_p1_pronoss = Doctrine::getTable('PronosP1Pronos')
		  ->createQuery('a')
		  ->where('user_id = '.$user->getGuardUser()->getId() )
		  ->execute();	
	}

  }

  public function executeShow(sfWebRequest $request)
  {
    $this->pronos_p1_pronos = Doctrine::getTable('PronosP1Pronos')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->pronos_p1_pronos);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new PronosP1PronosForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $user = $this->getUser();
    $this->forward404Unless($request->isMethod(sfRequest::POST));

	$list = $request->getParameter('pronos_p1_pronos');
	foreach($list as $item) {
		$p1match_id = $item["p1match_id"];
		$user_id = $item["user_id"];
		$score1 = $item["score1"];
		$score2 = $item["score2"];
		
		$newPronos = Doctrine::getTable('PronosP1Pronos')
						->createQuery('a')
						->where('user_id = '.$user_id.' and p1match_id = '.$p1match_id)
						->fetchOne();
		if($newPronos == null && $score1 != "" && $score2 != "")
			$newPronos = new PronosP1Pronos();
		
		if($newPronos != null) {
			$newPronos->setUserId($user_id);
			$newPronos->setP1matchId($p1match_id);
			$newPronos->setScore1($score1);
			$newPronos->setScore2($score2);
			
			if($newPronos->isValid()) {
				$newPronos->save();
			}
		}
	}
	
    //$this->form = new PronosP1PronosForm();
    //$this->processForm($request, $this->form);
	
	$this->pronos_p1_pronoss = Doctrine::getTable('PronosP1Pronos')
		  ->createQuery('a')
		  ->where('user_id = '.$user->getGuardUser()->getId() )
		  ->execute();	
		
		$day = idate("d");
		$month = idate("m");
		$hr = 1 + idate("H");
		$min = idate("i");
		$today = date('Y-m-d H:i', mktime($hr,$min,0,$month,$day, 2010));
				
		$this->matchs_date = Doctrine::getTable('PronosP1Match')
		  ->createQuery('a')
		  ->where("a.date > '".$today."'")
		  ->limit(10)
		  ->orderby('date')
		  ->execute();
	
    $this->setTemplate('edit');
	
	
	
  }

  public function executeEdit(sfWebRequest $request)
  {
	$user = $this->getUser();
	$this->forward404Unless($user->isAuthenticated());
    //$this->forward404Unless($pronos_p1_pronos = Doctrine::getTable('PronosP1Pronos')->find(array($request->getParameter('id'))), sprintf('Object pronos_p1_pronos does not exist (%s).', $request->getParameter('id')));
    //$this->form = new PronosP1PronosForm($pronos_p1_pronos);
	
	if($user->hasCredential('admin') ) {
		$this->pronos_p1_pronoss = Doctrine::getTable('PronosP1Pronos')
		  ->createQuery('a')
		  ->execute();	
	} else {
		$this->pronos_p1_pronoss = Doctrine::getTable('PronosP1Pronos')
		  ->createQuery('a')
		  ->where('user_id = '.$user->getGuardUser()->getId() )
		  ->execute();	
		
		$day = idate("d");
		$month = idate("m");
		$hr = 1 + idate("H");
		$min = idate("i");
		$today = date('Y-m-d H:i', mktime($hr,$min,0,$month,$day, 2010));
				
		$this->matchs_date = Doctrine::getTable('PronosP1Match')
		  ->createQuery('a')
		  ->where("a.date > '".$today."'")
		  //->limit(10)
		  ->orderby('date')
		  ->execute();
	}
	
	$a = $request->getParameter('id');
	
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($pronos_p1_pronos = Doctrine::getTable('PronosP1Pronos')->find(array($request->getParameter('id'))), sprintf('Object pronos_p1_pronos does not exist (%s).', $request->getParameter('id')));
    $this->form = new PronosP1PronosForm($pronos_p1_pronos);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($pronos_p1_pronos = Doctrine::getTable('PronosP1Pronos')->find(array($request->getParameter('id'))), sprintf('Object pronos_p1_pronos does not exist (%s).', $request->getParameter('id')));
    $pronos_p1_pronos->delete();

    $this->redirect('p1pronos/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $pronos_p1_pronos = $form->save();

      $this->redirect('p1pronos/edit?id='.$pronos_p1_pronos->getId());
    }
  }
}
